<%@ page language="java" import="beans.*, util.*, java.util.*" %>
<html><html xmlns="http://www.w3.org/1999/xhtml">
    <head>
	<link type="text/css" href="1.css" rel="stylesheet" media="screen" />
        <title>Delete Model Page</title>
    </head> 
    <%            
        String msg = util.Utilities.isCorrectStaffLogin(session);
        String name = (String)session.getAttribute("username");
        if(msg != null) {
            out.println(msg);
        }else {
    %>
	<tr>
      <div id=banner>
        <td width="182" height="69"><img src="../image/cpt_hook_banner1.jpg" width="123" height="87" />&nbsp;</td>
 
    <B>Hi!<%=name%></B>
    <p><a href="logout.jsp">Click here to logout</a></p>
   	</td>
	</div>
	</tr>
	<center><h1>Delete Model Page</h1></center>
    <body>
	<tr>
      <td height="31" colspan="3" id="left">
	
                <table width="200" border="0">
                  <tr>
                    <td width="94"><tr>
      			
                      <td width="90"><div>
                <SCRIPT src="displayMyFlash.js"></SCRIPT></div><div id="content"><table align="center" border="1">
                      <tr bgcolor="white"><b>
                        <td>Item ID:</td>
                        <td>Name</td>
                        <td>Type</td>
                        <td>Company</td>
                        <td>Scale</td>
                        <td>Price</td>
                        <td>Description</td>
                        <td>Image</td>
                        <td>Quantity</td>
                        <td>Delete</td>
                      </b> </tr>
                      <%
                        
                        
                        final String sPage = (String)request.getParameter("page");
                        int page1 = 0; //<====
                        final int recordPerPage = 5;
                        final int indexPerPage =10;
                        final int count = ModelBean.getTotalRecordCount(); //<=======
                        final int indexCount = (int)Math.floor(count / (double)recordPerPage);
                        if(page1 > indexCount) {
                            page1 = 0;
                        }
                        if(util.Utilities.isValid(sPage)) {
                            try {
                                page1 = Integer.parseInt(sPage);
                            } catch(Exception e) {
                                
                            }
                        }
                        final int k=page1;
                        
                        
                        %>
                      <%
                        
                        try {
                            ModelBean[] models = ModelBean.getAllModel();
                            for(int z=recordPerPage*page1; z<(page1+1)*recordPerPage; z++ ) {
                                
                                String itemId = models[z].getItemID();
                                String name1 = models[z].getName();
                                String type = models[z].getType();
                                String company = models[z].getCompany();
                                String scale = models[z].getScale();
                                String price = models[z].getPrice();
                                String description = models[z].getDescription();
                                String imageURL = models[z].getImageURL();
                                int quantity = models[z].getQuantity();
                        %>
                      <form action='DeleteServlet' method='get'>
                        <tr>
                          <td><%=itemId%>
                              <input type='hidden' name='itemID2' value='<%=itemId%>' />
                          </td>
                          <td><%=name1%>
                              <input type='hidden' name='name2' value='<%=name1%>' />
                          </td>
                          <td><%=type%>
                              <input type='hidden' name='type2' value='<%=type%>' />
                          </td>
                          <td><%=company%>
                              <input type='hidden' name='company2' value='<%=company%>' />
                          </td>
                          <td><%=scale%>
                              <input type='hidden' name='scale2' value='<%=scale%>' />
                          </td>
                          <td> $<%=price%>
                              <input type='hidden' name='price2' value='<%=price%>' />
                          </td>
                          <td><%=description%>
                              <input type='hidden' name='description2' value='<%=description%>' />
                          </td>
                          <td><a href='photo.jsp?imageURL=<%=imageURL%>'><img src='../image/<%=imageURL%>' width=150 height=200/></a>
                              <input type='hidden' name='imageURL2' value='<%=imageURL%>' />
                          </td>
                          <td><%=quantity%>
                              <input type='hidden' name=' quantity2' value='<%=quantity%>' />
                          </td>
                          <td valign='top'><input name="Submit" type='Submit' value='Delete'>
                          </td>
                        </tr>
                      </form>
                      <%
                            }
                        } catch(Exception e) {
                            e.printStackTrace();
                        }
                        %>
                      <%!                                
                        void printIndex(int k, int i, JspWriter out1) {
                            try {
                                if (i==k) {
                                    out1.println("<b><a href='DeleteModel.jsp?page="+i+"'>"+(i+1)+"</a></b>");
                                }else {
                                    out1.println("<a href='DeleteModel.jsp?page="+i+"'>"+(i+1)+"</a>");
                                }
                            }catch(Exception e) {
                                
                            }
                        }
                        
                        %>
                    </table></div></td>
                  </tr>
                </table>
		</td>
	</tr>
        <form method='get' action='DeleteModel.jsp'>
            
            <center>
			 <td height="44" colspan="3" id="footer"><h1>
                <%
                            int nextValue = 5;
                            int remain = count-(indexCount+page1);
                            if (remain < indexPerPage) {
                                nextValue = remain % indexPerPage;
                            }
                            if(k < 5) {
                                for(int i=0; i<=indexCount&&i<indexPerPage; i++) {
                                    printIndex(k, i, out);
                                }
                            }else if(k >= indexCount-indexCount%indexPerPage -1) {
                                int start = indexCount - indexPerPage;
                                for(int y=0; y <indexPerPage; y++) {
                                    printIndex(k, y+start, out);
                                }
                            }else {
                                for (int i=0; i<5; i++) {
                                    printIndex(k, page1-5+i, out);
                                }
                                for (int i=0; i< 5&& page1+i<indexCount; i++) {
                                    printIndex(k, page1+i, out);
                                }
                            }
                            %>
                <input type='hidden' name='page' value='<%=k+1%>' />
                <input type = submit value='Next Page'/>
				</h1>
            </center>
        </form> 
        <%
        }%>
    </body>
</html>